<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!showAddress" class="none">
        您需要先添加收货地址才可提交订单。
      </div>
      <template v-else>
        <ul>
          <li>
            <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
          </li>
          <li>
            <span>联系方式：</span>{{ contactSecret(showAddress.contact) }}
          </li>
          <li>
            <span>收货地址：</span
            >{{ showAddress.fullLocation + showAddress.address }}
          </li>
        </ul>
        <a href="javascript:;" @click="addressEditCom.open(showAddress)"
          >修改地址</a
        >
      </template>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="dialogVisible = true">切换地址</XtxButton>
      <XtxButton class="btn" @click="addressEditCom.open()">添加地址</XtxButton>
    </div>
  </div>

  <!-- 对话框组件测试 -->
  <XtxDialog title="切换收货地址" v-model="dialogVisible">
    <div
      class="text item"
      @click="selectedAddress = item"
      :class="{ active: selectedAddress === item }"
      v-for="item in list"
      :key="item"
    >
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ item.receiver }}
        </li>
        <li><span>联系方式：</span>{{ contactSecret(item.contact) }}</li>
        <li><span>收货地址：</span>{{ item.fullLocation + item.address }}</li>
      </ul>
      <div class="delIcon" @click="delAress(item.id)"><i class="iconfont icon-close-new"></i></div>
    </div>
    <template #footer>
      <XtxButton
        type="gray"
        style="margin-right: 20px"
        @click="dialogVisible = false"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="confirmAddress">确认</XtxButton>
    </template>
  </XtxDialog>

  <!-- 把新增和修改的 XtxDialog 作为子组件了 -->
  <AddressEdit @on-success="onSuccess" ref="addressEditCom"></AddressEdit>
</template>

<script>
import { inject, ref } from 'vue'
import Message from '@/components/library/Message'
import AddressEdit from './address-edit.vue'
import { deleteAddress } from '@/api/order'
import Confirm from '@/components/library/Confirm'
export default {
  components: { AddressEdit },
  name: 'CheckoutAddress',
  emits: ['change'],
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup (props, { emit }) {
    // 是否显示对话框
    const dialogVisible = ref(false)

    // 高亮的收货地址
    const selectedAddress = ref(props.list[0])

    // 在页面中显示的收货地址
    const showAddress = ref(props.list[0])

    const getAddressList = inject('getAddressList')

    // 手机号保密
    const contactSecret = (contact) => {
      // 最终格式：135****7057
      // 正则替换：用括号匹配到的可以用 $1 访问到
      // return contact.replace(/(\d{3})(\d{3})(\d{4})/, '$1****$2')
      return contact.slice(0, 3) + '****' + contact.slice(0, 4)
    }

    // 默认展示地址，字段中有一个 isDefault: 0 表示默认收货地址，但是后台还没完善，所以我们先展示地址数组的第一个
    const getDefaultAddress = props.list.find((item) => item.isDefault === 0)
    if (getDefaultAddress) {
      // 页面中展示的地址
      showAddress.value = getDefaultAddress
      // 高亮效果的地址
      selectedAddress.value = getDefaultAddress
    }

    // 如果有默认收货地址，加载组件的时候就把默认收货地址的id传给父组件
    emit('change', showAddress.value && showAddress.value.id)

    // 点击确认按钮，确认收货地址
    const confirmAddress = () => {
      // 如果没有选择收货地址，就提示
      if (!selectedAddress.value) {
        return Message({ type: 'warn', message: '请选择收货地址' })
      }
      // 把选中的地址存储起来，用于页面展示
      showAddress.value = selectedAddress.value
      // 关闭弹出框
      dialogVisible.value = false
      // 通知父组件接收到地址 id，因为下单接口需要地址 id
      emit('change', showAddress.value.id)
    }

    const onSuccess = (theForm) => {
      console.log(theForm)
      // 展开响应式对象，让他变回普通对象展示即可
      showAddress.value = { ...theForm }
    }

    // 删除地址
    const delAress = (id) => {
      Confirm({ title: '温馨提示', text: '亲，是否删除该地址吗?' }).then(async () => {
        await deleteAddress(id)
        Message({ type: 'success', message: '删除成功' })
        getAddressList()
      })
    }

    const addressEditCom = ref(null)

    return {
      dialogVisible,
      selectedAddress,
      contactSecret,
      showAddress,
      confirmAddress,
      addressEditCom,
      onSuccess,
      delAress
    }
  }
}
</script>

<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      position: relative;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
        position: relative;
      }
      .delIcon {
        position: absolute;
        right: 50px;
      }
    }
  }
}
</style>
